/* Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package com.gp.newsstand.auth.client;

import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Request;
import org.w3c.dom.Document;

public class GpAuthClient {

  private static final String ERROR_MSG =
      "Sorry - your email address and password do not match our records.";
  private static final String SUCCESS_MSG = "Success";

  public static class Response {
    public final boolean success;
    public final String reason;
    public final String token;

    public Response(boolean success, String reason, String token) {
      this.success = success;
      this.reason = reason;
      this.token = token;
    }
  }

  public Response authentication(String name, String password) throws GpException {
    Response response;
    try {
      // TODO: Make the correct login request to the auth server.
      Content c = Request
          .Post("https://registration.genericpublication.com/registration/login/mobile/login?thirdparty=google")
          .addHeader("user-agent", "Google")
          .addHeader("Accept", "*/*")
          .bodyForm(Form.form().add("name", name).add("password", password).build())
          .execute()
          .returnContent();
      // TODO: Parse the response and get the token.
      boolean success = false;
      String reason = "";
      String token = null;
      // TODO: Construct and return a Response object.
      if (success) {
        reason = SUCCESS_MSG;
      } else {
        reason = ERROR_MSG;
      }
      response = new Response(success, reason, token);
    } catch (Exception e) {
      throw new GpException(e);
    }
    return response;
  }

}
